package com.fas.system.util;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fas.system.domain.FasBuildingCategory;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 雪花ID生成
 *
 * @author Auler_zrl
 */
public class Test {

    /**
     * 测试
     */
    public static void main(String[] args) {
        String str = "1111";
        String[] split = str.split(",");

        System.out.println(Arrays.toString(split));

    }

    public static void get(String url) {

        // 获取连接客户端工具
        CloseableHttpClient httpClient = HttpClients.createDefault();

        String entityStr = null;
        CloseableHttpResponse response = null;

        try {
            /*
             * 由于GET请求的参数都是拼装在URL地址后方，所以我们要构建一个URL，带参数
             */
            URIBuilder uriBuilder = new URIBuilder("https://119.9ancore.com/admin/dicCompanyCategory/findFullData");
            // 根据带参数的URI对象构建GET请求对象
            HttpGet httpGet = new HttpGet(uriBuilder.build());

            /*
             * 添加请求头信息
             */
            // 浏览器表示
            httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
            // 传输的类型
            httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
            httpGet.addHeader("authorization","Bearer e5037751-f9d9-4424-bac3-4a447dfa95f3");
            // 执行请求
            response = httpClient.execute(httpGet);
            // 获得响应的实体对象
            HttpEntity entity = response.getEntity();
            // 使用Apache提供的工具类进行转换成字符串
            entityStr = EntityUtils.toString(entity, "UTF-8");
        } catch (ClientProtocolException e) {
            System.err.println("Http协议出现问题");
            e.printStackTrace();
        } catch (URISyntaxException e) {
            System.err.println("URI解析异常");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IO异常");
            e.printStackTrace();
        } finally {
            // 释放连接
            if (null != response) {
                try {
                    response.close();
                    httpClient.close();
                } catch (IOException e) {
                    System.err.println("释放连接出错");
                    e.printStackTrace();
                }
            }
        }
        // 打印响应内容
        JSONObject jsonObject = JSONObject.parseObject(entityStr);
        JSONArray data = jsonObject.getJSONArray("data");
//        JSONArray s = listToTree(data, "id", "parentId", "children");
//        System.err.println(s);
        List<FasBuildingCategory> fasBuildingCategories = treeToList(data, null);
        System.err.println(fasBuildingCategories);
    }

    /**
     * 树形结构转list
     *
     * @param
     * @return
     */
    private static List<FasBuildingCategory> treeToList(JSONArray depts, String pid) {

        List<FasBuildingCategory> resultList = new ArrayList<>();
        for (int i = 0; i < depts.size(); i++) {
            JSONObject jsonObject = depts.getJSONObject(i);
            jsonObject.put("parentId",pid);
            if (jsonObject.getJSONArray("children") != null && jsonObject.getJSONArray("children").size() > 0){
                resultList.addAll(treeToList(jsonObject.getJSONArray("children"),jsonObject.getString("id")));
                jsonObject.put("children",null);
                resultList.add(JSONObject.toJavaObject(jsonObject,FasBuildingCategory.class));
            }else {
                resultList.add(JSONObject.toJavaObject(jsonObject,FasBuildingCategory.class));

            }
        }
        return resultList;

    }

}